<?php

include dirname(dirname(__FILE__)) . '/include/common.php';

class webServiceGetUser extends core {

    public function process() {
        $user_id = intval($_POST['user_id']);
        $email = $this->_cleanEmail($_POST['email']);
        $data = $this->db->get_row("SELECT * FROM users WHERE `id`='$user_id' AND email='$email'");

        if ($data) {
           $event = $this->db->get_row("SELECT count(event_id) as total FROM events WHERE `user_id`='$user_id'");
           if($event){
               $total_event =  $event->total;
           }else{
               $total_event = 0;
           }
           $guest = $this->db->get_row("SELECT DISTINCT COUNT(guest_id) AS total FROM guest_event WHERE event_id IN (SELECT event_id FROM events WHERE user_id =$user_id)");
           
           if($guest){
               $total_guest =  $guest->total;
           }else{
               $total_guest = 0;
           }
            $todo = $this->db->get_results("SELECT count(todo_id) as total FROM todo_lists WHERE `user_id`='$user_id' AND is_done =1
                                        UNION SELECT count(todo_id) as total FROM todo_lists WHERE `user_id`='$user_id'");
           if($todo){
               if(count($todo)==2){
                $str_todo=$todo[0]->total."/".$todo[1]->total;
               }else{
                   $str_todo = "0/0";
               }
              
           }else{
               $str_todo = 0;
           }
           $result_spent = $this->db->get_row("SELECT SUM(item_amount) AS total_spent, budget_goal FROM budget_items,users WHERE user_id=$user_id AND budget_items.user_id = users.id");
            $spent = $result_spent->total_spent;
            $goal = $result_spent->budget_goal;
            $unused_money = $goal - $spent;
            $format_unused_money = number_format($unused_money,2);
            $this->_JSONResponse(array('error' => 0, 'total_event' => $total_event, 
                'total_guest' => $total_guest,
                'money'=>$format_unused_money,
               'todo'=>$str_todo,
                'male'=>$data->first_name,
                'female'=>$data->fiance_first_name
                ));
        } else {
            $this->_JSONResponse(array('error' => 1,'msg'=>'User does not exist'));
        }
    }

}

$processor = new webServiceGetUser();
$processor->process();
?>